package com.example.administrator.myandroidproject;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.List;

/**
 * Created by Administrator on 2017/3/29.
 */

public class ClockView extends View {
    private static final String TAG = "zsr";
    private Paint mCirclePaint;
    private Paint mDegreePaint;
    private Paint mTextPaint;
    private Paint mHourPaint;
    private Paint mMinPaint;
    private int mOriginWidth = 200,mCircleWidth = 100;

    public ClockView(Context context) {
        super(context);
    }

    public ClockView(Context context, AttributeSet attrs) {

        super(context, attrs);
        mCirclePaint = new Paint();
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setStyle(Paint.Style.STROKE);
        mCirclePaint.setStrokeWidth(3);


        mDegreePaint = new Paint();
        mDegreePaint.setAntiAlias(true);

        mTextPaint = new Paint();

        mHourPaint = new Paint();
        mHourPaint.setStrokeWidth(8);

        mMinPaint = new Paint();
        mMinPaint.setStrokeWidth(6);


    }

    public ClockView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);


    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(mOriginWidth, mOriginWidth,mCircleWidth,mCirclePaint);
        int width = mOriginWidth - mCircleWidth;
        for (int i = 0; i < 24; i++) {
            if ( i == 0 || i == 6 || i == 12 || i == 18){ //0,3,6,9
                mDegreePaint.setStrokeWidth(4);
                mDegreePaint.setTextSize(15);
                canvas.drawLine(mOriginWidth, width,mOriginWidth, width + 30,mDegreePaint);
            }else{
                mDegreePaint.setStrokeWidth(3);
                mDegreePaint.setTextSize(15);
                canvas.drawLine(mOriginWidth, width,mOriginWidth,width + 20,mDegreePaint);
            }

            canvas.rotate(15,mOriginWidth,mOriginWidth);
        }



        canvas.save();// 保存上次的canvas，即对下面的不影响

        canvas.translate(mOriginWidth,mOriginWidth);
        canvas.drawLine(0,0,40,0,mHourPaint);
        canvas.drawLine(0,0,50,30,mMinPaint);

        canvas.save();
        for (int i = 0; i < 12; i+=3) {
            String degress = String.valueOf(i);
            Paint.FontMetrics metrics = mTextPaint.getFontMetrics();
            float textWidth = mTextPaint.measureText(degress);
            float textHeight = (metrics.descent-metrics.ascent)/2;
            Log.d(TAG, "onDraw: "+textHeight+" "+textWidth);
            float dx = 0;
            float dy = 0;
            switch(i){
                case 0:
                    dx = -textWidth/2;
                    dy = -mCircleWidth + textHeight*2+30;
                    break;
                case 3:
                    dx = mCircleWidth - 30 - textWidth - 10;
                    dy = textHeight;
                    break;
            }




            canvas.drawText(degress,dx,dy,mTextPaint);
            canvas.restore();
        }


        canvas.restore();  //和上次save的canvas结合起来

    }
}
